package com.zdxk.msgPush.stockMsg.controller;

import com.alibaba.fastjson.JSONObject;
import com.zdxk.msgPush.stockMsg.vo.StockBasicInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("stockBasic")
public class StockBasicController {

    @Autowired
    private RedisTemplate redisTemplate;
    
    @RequestMapping("putAllStockBasicInfoToRedis")
    public String putAllStockBasicInfoToRedis() throws IOException {
        List<StockBasicInfo> basicInfoList = new ArrayList<>();
        InputStream inputStream = StockBasicController.class.getClassLoader().getResourceAsStream("data/stockBasic.csv");
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String temp = null;
        br.readLine();
        while ((temp = br.readLine())!= null){
            String[] stockInfoArray = temp.split(";");
            basicInfoList.add(new StockBasicInfo(stockInfoArray[0],stockInfoArray[2],stockInfoArray[11]));
        }
        redisTemplate.opsForList().leftPushAll("stockbasic",basicInfoList.toArray());
        return "ok";
    }

    @RequestMapping("getAllStockBasicInfo")
    @ResponseBody
    public String getAllStockBasicInfo() throws IOException {
        List list = redisTemplate.opsForList().range("stockbasic",0,10);
        return JSONObject.toJSONString(list);
    }
}
